<?php

namespace App\Http\Controllers\Api;

use App\Repositories\OrderRepository;
use App\Repositories\WechatRepository;
use App\Repositories\RechargeComboRepository;
use App\Repositories\Api\MemberRepository;

use App\Models\Group;
use App\Models\Order;
use App\Models\Member;
use App\Models\ElectPool;
use App\Models\KeywordRecord;
use Illuminate\Support\Facades\Log;

use Exception;
use Cache;

class OrderController extends MiniAppController
{
    private $memberRep = NULL,
        $comboRep = NULL;

    public function __construct(
        MemberRepository        $memberRep,
        RechargeComboRepository $comboRep,
        OrderRepository         $orderRep
    )
    {
        parent::__construct();

        $this->memberRep = $memberRep;
        $this->comboRep = $comboRep;
        $this->orderRep = $orderRep;
    }

    /**
     * 充值
     * @Author   thomashtao               bosslen
     * @DateTime 2022-03-28T15:33:25+0800
     * @return   [type]                   [description]
     */
    public function recharge()
    {
        $params = request()->only("id", "wechat_admin_id");

        $combos = $this->comboRep->get_recharge_prices($params['wechat_admin_id'], 1);
        // $combo_ids = get_column($combos, "id");
        if (!in_array($params['id'], get_column($combos, "id"))) {
            return $this->apiResponse->error("参数检验错误", 400);
        }
        $combos = ass_column($combos, "id");

        //生成订单
        $data['total_price'] = $combos[$params['id']]['price'];
        $data['type'] = 2; //充值
        $data['title'] = "充值" . $data['total_price'];
        $data['product_id'] = $params['id'];
        $data['refund_price'] = 0.00;
        $data['platform'] = 'weapp';

        $member = $this->memberRep->getOne($params['wechat_admin_id']);
        $qr_code_id = KeywordRecord::where(['unionid' => $member['unionid'], 'is_new' => 1])->value('qr_code_id');
        if ($qr_code_id)
            $data['resource'] = $qr_code_id;
        $order = $this->orderRep->generateOrder($data);

        return $this->apiResponse->success(['out_trade_no' => $order['out_trade_no']]);
    }

    public function pay_param()
    {
        $params = request()->only("order_id", "wechat_admin_id");
        $member = $this->memberRep->getOne(UID);
        $order = $this->orderRep->getByOutTradeNo($params['order_id']);

        if (UID == 101524) {
            $order['total_price'] = 0.01;
        }

        try {
            $wechatRep = new WechatRepository();
            $res = $wechatRep->unifiedOrder([
                'out_trade_no' => $order['out_trade_no'],
                'body' => $order['title'],
                'openid' => $member['weapp_openid'],
                'total_price' => $order['total_price'],
            ]);

            return $this->apiResponse->success([
                'package' => $res['package'],
                'paySign' => $res['paySign'],
                'timeStamp' => strval(time()),
                'nonceStr' => $res['nonceStr'],
            ]);

        } catch (Exception $e) {
            return $this->apiResponse->error($e->getMessage(), 400);
        }
    }

    /**
     * 详情
     * @Author   thomashtao               bosslen
     * @DateTime 2022-04-18T16:16:47+0800
     * @return   [type]                   [description]
     */
    public function detail()
    {
        $out_trade_no = trim(request()->get("order_id"));
        if (!$out_trade_no)
            return $this->apiResponse->error("参数错误");

        $orderRep = new OrderRepository();
        $order = $orderRep->getByOutTradeNo($out_trade_no);
        $member = $this->memberRep->getOne(UID);

        if ($order['type']) {
            $account = $this->memberRep->get_account(UID); //账户余额
            $dikou = get_dikou($account, $order['original_price']); //抵扣
            $shengyu = get_account_shengyu($account, $dikou);//剩余

            $order['dikou'] = $dikou;
            $order['shengyu'] = $shengyu;
            $order['account'] = $account;
        }

        return $this->apiResponse->success([
            'order' => $order,
            'member' => $member
        ]);
    }

    /**
     * 抵扣支付
     * @Author   thomashtao               bosslen
     * @DateTime 2022-04-19T17:36:53+0800
     * @return   [type]                   [description]
     */
    public function dikou()
    {
        $out_trade_no = request()->get("order_id", 0);
        $order = $this->orderRep->getByOutTradeNo($out_trade_no);
        if (!$order)
            return $this->apiResponse->error("参数错误");
        //抵扣
        $account = $this->memberRep->get_account(UID);
        if ($account > 0) {
            $real_pay = get_real_pay($account, $order['original_price']);
            $dikou = get_dikou($account, $order['original_price']);
            $order['total_price'] = $real_pay;
            Order::where("id", "=", $order['id'])->update(['total_price' => $real_pay]);
        }

        $res = $this->orderRep->updateOrder(date("ymdhis") . GenSecret(8, 1), $order['out_trade_no']);
        if ($res) {
            return $this->apiResponse->success([]);
        } else {
            return $this->apiResponse->error("抵扣失败");
        }
    }

    public function test()
    {
        echo "weapp  setting  success";


    }

}
